<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
	
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('games');
	}
	

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	// return array of leagues ( groped )
	public function getUniqueLeagues($arrayOfGames)
	{
		
		$leagues=array();
		foreach ($arrayOfGames as $item)
		{
			
			
				if (!(in_array($item['league'], $leagues))){
					array_push($leagues, $item['league']);			
				}
			
		}
		return $leagues;
	}
	
	public function filterNotFinished($arrayOfGames)
	{
		$arrayHelp= array();
		foreach ($arrayOfGames as $item)
		{
			
			if ($item['result']==' - '){
			array_push($arrayHelp, $item);
			}
			
		}
		return $arrayHelp;
		
	}
	
	public function getRecomended($arrayOfGames,$recMin)
	{
		$arrayRecomended= array();
		foreach ($arrayOfGames as $item) {
			if ($item['procenatFix1']>$recMin){
				array_push($arrayRecomended, $item);
			}
		}
		return $arrayRecomended;
	}
	
	// stara funkcija potrebna ukoliko se odavere da podaci idu direktno iz parsiranja
	// return games with percent > $recMin
	public function getRecomendedX($arrayOfGames,$recMin)
	{
		$arrayRecomended= array();
		foreach ($arrayOfGames as $item) {
			if ($item->procenatFix1>$recMin){
				array_push($arrayRecomended, $item);				
			}
		}
		return $arrayRecomended;
	}
	// stara funkcija potrebna ukoliko se odavere da podaci idu direktno iz parsiranja
	public function getUniqueLeaguesX($arrayOfGames)
	{
	
		$leagues=array();
		foreach ($arrayOfGames as $item)
		{
				
				
			if (!(in_array($item->league, $leagues))){
				array_push($leagues, $item->league);
			}
				
		}
		return $leagues;
	}
	
	public function index()
	{
		$loadData = "db"; // postavi "db", kako bi se lodovali podaci iz baze a ne parsirani podaci
		$arrayOfGames;
		$data;
		if ($loadData=="db")
		{	
			//$arrayOfGames = $this->games->loadLastDateGames();
			$arrayOfGames = $this->filterNotFinished($this->games->loadLastDateGames());
			
			$data['games']=$arrayOfGames;
			$data['leagues']=$this->getUniqueLeagues($arrayOfGames);
			$data['recomended']=$this->getRecomended($arrayOfGames,70);
			$this->load->view('welcome_message',$data);
		}
		else 
		{
			$this->load->library('prosoccerParser');
			$parser= new ProsoccerParser();
			$arrayOfGames = $parser->parseMainPage('http://www.prosoccer.gr');
			// naravno netreba odavde da se printa nego da se salje na view
			//echo '<pre>';
			//print_r($arrayOfGames);
			//echo '</pre>';
			//print_r( $this->getUniqueLeagues($arrayOfGames));
			// sortiranje po zemljama tj po prvom parametru, verovatno sortira i po drugim parametrima
			//asort($arrayOfGames);
			$data['games']=$arrayOfGames;
			$data['leagues']=$this->getUniqueLeaguesX($arrayOfGames);
			$data['recomended']=$this->getRecomendedX($arrayOfGames,70);
			$this->load->view('welcome_messageX',$data);
		}
		
		
		//echo '<pre>';
		//print_r($arrayOfGames);
		//echo '</pre>';
		
		
		
		
		//echo $arrayOfGames[0]->league;
		
		
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */